package com.jdd.fintech.morse.plugin.util;

import com.google.common.collect.Maps;
import com.jd.fastjson.JSONObject;
import com.jdd.fintech.morse.plugin.MoresOpenApiDecryptsFilter;
import com.jdd.fintech.morse.plugin.MoresOpenApiTokenFilter;
import com.jdd.fintech.morse.plugin.MorseOpenApiEncryptsFilter;
import com.jdd.fintech.morse.plugin.constans.OpenApiConstans;
import com.jdd.fintech.morse.plugin.enums.BooleanEnum;
import com.jdd.fintech.morse.plugin.enums.CustomVersionEnum;
import com.jdd.fintech.morse.plugin.enums.EncrAndDecrAlgorithmTypeEnum;
import com.jdd.fintech.morse.plugin.enums.RequestDateDirectionEnum;
import com.jddglobal.engine.boot.ImmutableChain;
import com.jddglobal.filter.Filter;
import com.jddglobal.filter.data.Context;
import com.jddglobal.filter.data.RemoteCommand;
import com.jddglobal.filter.data.Resource;
import com.jddglobal.util.bean.BeanMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import static com.jdd.fintech.morse.plugin.constans.OpenApiConstans.*;
import static com.jdd.fintech.morse.plugin.enums.CustomVersionEnum.YGC;
import static com.jdd.fintech.morse.plugin.enums.EncrAndDecrAlgorithmTypeEnum.PRIVATE_RSA;
import static com.jdd.fintech.morse.plugin.enums.EncrAndDecrAlgorithmTypeEnum.PUBLIC_RSA;
import static com.jdd.fintech.morse.plugin.enums.RequestDateDirectionEnum.REQUEST;
import static com.jdd.fintech.morse.plugin.util.RSAUtil.*;

/**
 * @description: 请求参数构造---工具类
 * @date: 2022/4/14 10:39
 * @author: fengcheng
 **/
public class RequestParameterCus {

    /**
     * 构造请求参数
     * @return
     */
    public static Context contextMorseEncrypts (String requestPara) {
        Context context = new Context(Maps.newHashMap());
        RemoteCommand remoteCommand = new RemoteCommand();
        RemoteCommand.RemoteDefinition definition = new RemoteCommand.RemoteDefinition();
        definition.setRpcType("HTTP");
        definition.setUri("http://172.16.40.181:80/oauth/oauth2/access_token");
        remoteCommand.setDefinition(definition);
        context.setRemoteCommand(remoteCommand);
        Context.Common common = context.getCommon();
        common.setApiIdentify("POST:/api/morse_xin_betaf4/JT001/creditApply");
        Context.ApiInfo api = new Context.ApiInfo();
        api.setAddress("/api/morse_xin_betaf4/JT001/creditApply");
        context.api(api);
        context.getRequest().setBody(requestPara);
        context.getResponse().setBody(Resource.<Object>OK(requestPara));
        return context;
    }

    /**
     * filter chain config.
     * @return
     */
    public static ImmutableChain.Invoker invokerRemoteToken() {
        TreeSet<ImmutableChain.FilterChainSetting> filterSet = new TreeSet<>();
        filterSet.add(chainSetting(MoresOpenApiTokenFilter.class, 1, bulidMorseRemoteTokenPlugValue()));
        filterSet.add(chainSetting(MorseOpenApiEncryptsFilter.class, 2, bulidMorseEncryptPlugValue()));
        return ImmutableChain.Invoker.compile("move_chain", filterSet);
    }

    /**
     * filter chain config.
     * @return
     */
    public static ImmutableChain.Invoker invokerEncrypts() {
        TreeSet<ImmutableChain.FilterChainSetting> filterSet = new TreeSet<ImmutableChain.FilterChainSetting>();
        filterSet.add(chainSetting(MorseOpenApiEncryptsFilter.class, 1, bulidMorseEncryptPlugValue()));
        return ImmutableChain.Invoker.compile("move_chain", filterSet);
    }

    /**
     * filter chain config.
     * @return
     */
    public static ImmutableChain.Invoker invokerDecrypts() {
        TreeSet<ImmutableChain.FilterChainSetting> filterSet = new TreeSet<ImmutableChain.FilterChainSetting>();
        filterSet.add(chainSetting(MoresOpenApiDecryptsFilter.class, 1, bulidMorseDecryptPlugValue()));
        return ImmutableChain.Invoker.compile("move_chain", filterSet);
    }


    /**
     * filter info construct.
     * @param cls
     * @param order
     * @param <T>
     * @return
     */
    static <T extends Filter> ImmutableChain.FilterChainSetting chainSetting(Class<T> cls, int order, List<Filter.ConfigurationValue> filterValues) {
        ImmutableChain.FilterSetting filter = new ImmutableChain.FilterSetting();
        filter.setId((long)order);
        filter.setFilterClass(cls.getName());
        ImmutableChain.FilterChainSetting setting = BeanMapper.map(filter, ImmutableChain.FilterChainSetting.class);
        setting.setOrder(order);
        setting.setCls(cls);
        setting.setConfigValue(filterValues);
        setting.setFilterSetting(filter);
        return setting;
    }

    /**
     * 构建morse加密插件值
     * @return
     */
    private static List<Filter.ConfigurationValue> bulidMorseRemoteTokenPlugValue() {
        Filter.ConfigurationValue configurationValue = new Filter.ConfigurationValue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("customVersion", CustomVersionEnum.PAPH);                       //定制化版本枚举
        jsonObject.put("remoteToken", BooleanEnum.YES);                            //是否获取远程token
        jsonObject.put("tokenConfig", "{\"requestWay\": \"POST\",\"requestAddress\": \"http://172.16.40.181:80/oauth/oauth2/access_token\",\"requestParameter\": {\"grant_type\":\"client_credentials\",\"client_id\":\"P_PA002_ELIS_UWS\",\"client_secret\":\"znD5x4d1\"}}");  //加解密入口
        configurationValue.setSettingValue(jsonObject.toJSONString());
        List<Filter.ConfigurationValue> list = new ArrayList<Filter.ConfigurationValue>();
        list.add(configurationValue);
        return list;
    }

    /**
     * 构建morse加密插件值(云工厂)
     * @return
     */
    /*private static List<Filter.ConfigurationValue> bulidMorseEncryptPlugValue() {
        Filter.ConfigurationValue configurationValue = new Filter.ConfigurationValue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("encyAndDecyFlag", BooleanEnum.YES);                         //是否加解密
        jsonObject.put("encyAndDecyType", PRIVATE_RSA);                                     //加解密类型
        jsonObject.put("publicSecurity", ygcpublicSecurity);                                //加密密钥
        jsonObject.put("privateSecurity", "");                                      //解密密钥
        jsonObject.put("keySize", "");                                              //密钥长度
        jsonObject.put("salt", saltwd);                                             //盐值
        jsonObject.put("isComplete", BooleanEnum.NO);                               //是否完整加解密
        jsonObject.put("encryptMode", KEY_ALGORITHM);                               //加密模式
        jsonObject.put("customVersion", YGC);                                       //定制化版本枚举
        jsonObject.put("requestDateDirection", REQUEST);                            //加解密入口
        jsonObject.put("isFile", BooleanEnum.NO);                                   //是否是文件
        jsonObject.put("signKey", YGC_DATA_SIGN_KEY);                               //签名key
        jsonObject.put("dataContentKey", YGC_DATA_CONTENT_KEY);                     //业务报文key

        //私有非对称RSA加解密需要加入以下扩展信息
        //网贷使用云工厂公钥进行加密，插件扩展值
        JSONObject extendParameter = new JSONObject();
        extendParameter.put(YGC_SECRET_KEY, wdpublicSecurity);
        extendParameter.put(YGC_ENCRYPT_ALG, "aks-3des-cbc");
        extendParameter.put(YGC_SIGN_ALG, "aks-sha1");
        extendParameter.put(YGC_TIMESTAMP, PluginDateUtils.getYYYYMMddHHmmsssss());
        extendParameter.put(WD_PRIVATE_KEY, wdprivateSecurity);                         //网贷私钥
        jsonObject.put("extendParameter", extendParameter);                             //扩展对象

        configurationValue.setSettingValue(jsonObject.toJSONString());
        List<Filter.ConfigurationValue> list = new ArrayList<Filter.ConfigurationValue>();
        list.add(configurationValue);
        return list;
    }*/

    /**
     * 构建morse解密插件值(云工厂)
     * @return
     */
    /*private static List<Filter.ConfigurationValue> bulidMorseDecryptPlugValue() {
        Filter.ConfigurationValue configurationValue = new Filter.ConfigurationValue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("encyAndDecyFlag", BooleanEnum.YES);                         //是否加解密
        jsonObject.put("encyAndDecyType", PUBLIC_RSA);                                     //加解密类型
        jsonObject.put("publicSecurity", "");                                       //加密密钥
        jsonObject.put("privateSecurity", ygcprivateSecurity);                              //解密密钥
        jsonObject.put("keySize", "");                                              //密钥长度
        jsonObject.put("salt", saltygc);                                           //盐值
        jsonObject.put("isComplete", BooleanEnum.NO);                               //是否完整加解密
        jsonObject.put("encryptMode", PUBLIC_RSA);                                         //加密模式
        jsonObject.put("customVersion", YGC);                                       //定制化版本枚举
        jsonObject.put("requestDateDirection", REQUEST);                            //加解密入口
        jsonObject.put("isFile", BooleanEnum.NO);                                   //是否是文件
        jsonObject.put("signKey", YGC_DATA_SIGN_KEY);                               //签名key
        jsonObject.put("dataContentKey", YGC_DATA_CONTENT_KEY);                     //业务报文key
        *//*
        //公用非对称RSA加解密需要加入以下扩展信息
        //云工厂解密
        JSONObject extendParameter = new JSONObject();
        extendParameter.put(YGC_SIGN_ALG, MD_5_WITH_RSA);
        extendParameter.put(WD_PUBLIC_KEY, wdpublicSecurity);
        System.out.println("云工厂解密扩展对象：" + JSONObject.toJSONString(extendParameter));
        jsonObject.put("extendParameter", extendParameter);                         //扩展对象
        *//*
        configurationValue.setSettingValue(jsonObject.toJSONString());
        List<Filter.ConfigurationValue> list = new ArrayList<Filter.ConfigurationValue>();
        list.add(configurationValue);
        return list;
    }*/


    /**
     * 构建morse加密插件值
     * @return
     */
    private static List<Filter.ConfigurationValue> bulidMorseEncryptPlugValue() {
        Filter.ConfigurationValue configurationValue = new Filter.ConfigurationValue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("encyAndDecyFlag", BooleanEnum.YES);                         //是否加解密
        jsonObject.put("encyAndDecyType", EncrAndDecrAlgorithmTypeEnum.AES);                  //加解密类型
        jsonObject.put("publicSecurity", "1234561234561234");                       //加密密钥
        jsonObject.put("privateSecurity", "1234561234561234");                      //解密密钥
        jsonObject.put("keySize", "16");                                            //密钥长度
        jsonObject.put("salt", "789789");                                           //盐值
        jsonObject.put("isComplete", BooleanEnum.NO);                               //是否完整加解密
        jsonObject.put("encryptMode", OpenApiConstans.ENCRYPT_MODE_CBC);            //加密模式
        jsonObject.put("customVersion", CustomVersionEnum.G);                       //定制化版本枚举
        jsonObject.put("requestDateDirection", RequestDateDirectionEnum.REQUEST);  //加解密入口
        jsonObject.put("isFile", BooleanEnum.NO);                                  //是否是文件
//        jsonObject.put("dataContentKey", "infoContent");                                  //是否是文件
//        jsonObject.put("signKey", "infoSign");                                  //是否是文件
        configurationValue.setSettingValue(jsonObject.toJSONString());
        List<Filter.ConfigurationValue> list = new ArrayList<Filter.ConfigurationValue>();
        list.add(configurationValue);
        return list;
    }
    /**
     * 构建morse解密插件值
     * @return
     */
    private static List<Filter.ConfigurationValue> bulidMorseDecryptPlugValue() {
        Filter.ConfigurationValue configurationValue = new Filter.ConfigurationValue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("encyAndDecyFlag", BooleanEnum.YES);                         //是否加解密
        jsonObject.put("encyAndDecyType", EncrAndDecrAlgorithmTypeEnum.AES);                  //加解密类型
        jsonObject.put("publicSecurity", "1234561234561234");                       //加密密钥
        jsonObject.put("privateSecurity", "1234561234561234");                      //解密密钥
        jsonObject.put("keySize", "16");                                            //密钥长度
        jsonObject.put("salt", "789789");                                           //盐值
        jsonObject.put("isComplete", BooleanEnum.NO);                               //是否完整加解密
        jsonObject.put("encryptMode", OpenApiConstans.ENCRYPT_MODE_CBC);            //加密模式
        jsonObject.put("customVersion", CustomVersionEnum.PAPH);                       //定制化版本枚举
        jsonObject.put("requestDateDirection", RequestDateDirectionEnum.RESPONSE);   //加解密入口
        jsonObject.put("isFile", BooleanEnum.NO);                                  //是否是文件
//        jsonObject.put("dataContentKey", "infoContent");                                  //是否是文件
//        jsonObject.put("signKey", "infoSign");                                  //是否是文件
        configurationValue.setSettingValue(jsonObject.toJSONString());
        List<Filter.ConfigurationValue> list = new ArrayList<Filter.ConfigurationValue>();
        list.add(configurationValue);
        return list;
    }
}
